using UnityEngine;
using System.Collections;

namespace RichLabelSpace
{
	public abstract class RichLabelComponent
	{
        public RichComponentType m_component_type = RichComponentType.Unknow;
        public RichLabelEvent m_rich_event = null;
        public UIWidget m_ui_widget = null;
        protected Transform m_parent = null;
        public RichLabelCtrl m_ctrl = null;

        public Vector2 Size
        {
            get
            {
                if (m_ui_widget != null)
                    return m_ui_widget.localSize;
                else
                    return Vector2.zero;
            }
            set
            {
                if (m_ui_widget != null)
                {
                    m_ui_widget.height = (int)value.y;
                    m_ui_widget.width = (int)value.x;
                }
            }
        }

        public void SetPosition(Vector3 pos)
        {
            if(m_ui_widget != null)
                m_ui_widget.transform.localPosition = pos;
        }

        public bool AnalyticRichTxt(string rich_txt, RichComponentType component_type)
        {
            if (Analytic(rich_txt))
            {
                m_component_type = component_type;
                AnalyticEvent(rich_txt,this);
                return true;
            }

            return false;
        }

        protected virtual bool Analytic(string rich_txt)
        {
            string[] rich_txts = rich_txt.TrimStart(' ').TrimEnd(' ').Split('|');
            bool has_thing = false;
            if (rich_txts.Length > 0)
            {
                foreach (var txt in rich_txts)
                {
                    var strs = GetPropertyValue(txt);
                    if (strs != null && strs.Length == 2)
                    {
                        has_thing = true;
                        SetProperty(strs);
                    }
                }
            }

            return has_thing;
        }
        protected abstract void SetProperty(string[] strs);
        public abstract void Excute();

        public void ExcuteSelf()
        {
            if (this.m_ui_widget != null)
            {
                m_ui_widget.autoResizeBoxCollider = false;
                m_ui_widget.autoResizeBoxCollider = true;
            }
        }
        // 处理点击事件后的UI表现
        public virtual void OnClick() {}

        public void InitEvent()
        {
            m_rich_event.Init();
        }
        
        /// <summary>
        /// 解析属性方法
        /// 格式： text = 'some string!'
        /// </summary>
        /// <param name="txt"></param>
        /// <returns></returns>
        protected internal static string[] GetPropertyValue(string txt)
        {
            string[] property = new string[2];

            property = txt.Split('=');
            if (property.Length != 2)
            {
                return null;
            }

            // 移除等号，属性值开始和结束时的'号
            property[0] = property[0].TrimStart(' ').TrimEnd(' ');
            var temp_str = property[1];
            temp_str = temp_str.TrimStart(' ').TrimEnd(' ');
            if (temp_str.StartsWith("'"))
            {
                temp_str = temp_str.Remove(0, 1);
            }
            if (temp_str.EndsWith("'"))
            {
                temp_str = temp_str.Remove(temp_str.Length - 1, 1);
            }
            property[1] = temp_str;

            return property;
        }
        
        protected virtual void AnalyticEvent(string rich_txt,RichLabelComponent comp)
        {
            m_rich_event.AnalyticEvent(rich_txt, comp);
        }

        public RichLabelComponent(Transform parent,RichLabelCtrl ctrl) 
        {
            this.m_rich_event = new RichLabelEvent();
            this.m_parent = parent;
            this.m_ctrl = ctrl;
        }

        public virtual void Remove() 
        {
            // 暂时的处理方法，等到程序都优化结束后改进，放入到对象池中
            if (this.m_ui_widget != null)
            {
                this.m_ui_widget.gameObject.SetActive(false);
                this.m_ui_widget.transform.SetParent(null);
                GameObject.Destroy(this.m_ui_widget.gameObject);
            }
        }
	}
}
